package com.yaodian100.ec.generatepage.web;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.generate.entity.EventImage;
import com.yaodian100.ec.generate.entity.GenerateAdvertise;
import com.yaodian100.ec.generate.entity.GenerateContent;
import com.yaodian100.ec.generate.entity.GeneratePosition;
import com.yaodian100.ec.generate.service.GenerateService;

public class SpecialActAdvAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8280417163118221483L;

	private final Logger logger = LoggerFactory.getLogger(SpecialActAdvAction.class);
	private GenerateService generateService;
	private CategoryService categoryService;
	private GenerateAdvertise advertise;
	private GeneratePosition position;
	private GenerateContent content;
	private StringBuilder contents;
	private String advId;
	private String advName;
	private String advTitle;
	private String advType = "000_SPECIALACT";
	private String advStatus;
	private String startDate;
	private String endDate;
	private String startTime;
	private String endTime;
	private Date createDate;
	private String createUser;
	private Date modifyDate;
	private String modifyUser;
	private String mainCateId;
	private List<Category> categoryRoot;

	private File bannerImg = null;
	private String bannerImgName;
	private String bannerImgFolder;
	private String bannerImgLink;

	private String tab1ProdId;
	private String tab1Brief;
	private String tab1Name;
	private String tab2ProdId;
	private String tab2Brief;
	private String tab2Name;
	private String tab3ProdId;
	private String tab3Brief;
	private String tab3Name;
	private String tab4ProdId;
	private String tab4Brief;
	private String tab4Name;
	private String tab5ProdId;
	private String tab5Brief;
	private String tab5Name;
	private String tab6ProdId;
	private String tab6Brief;
	private String tab6Name;
	private String tab7ProdId;
	private String tab7Brief;
	private String tab7Name;
	private String tab8ProdId;
	private String tab8Brief;
	private String tab8Name;
	// private String tab9ProdId;
	// private String tab9Brief;
	// private String tab9Name;
	// private String tab10ProdId;
	// private String tab10Brief;
	// private String tab10Name;
	private int showSeq = 0;

	private EventImage eventImage = null;
	private String error;
	private final String lineBreak = "\n";

	/**
	 * 新增活动特辑
	 * 
	 * @return
	 */
	public String addSpecialActAdv() {
		advId = "";
		return "editSpecialActAdv";
	}

	/**
	 * 编辑活动特辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editSpecialActAdv() throws Exception {
		initVariables();
		advertise = generateService.get(Long.parseLong(advId));
		position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
		content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
		advStatus = advertise.getAdvStatus();
		advType = advertise.getAdvType();
		advName = advertise.getAdvName();
		startDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getStartDate());
		endDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getEndDate());
		DateFormat dateFormat = new SimpleDateFormat("HH:mm");
		startTime = dateFormat.format(advertise.getStartDate());
		endTime = dateFormat.format(advertise.getEndDate());
		createDate = advertise.getCreateDate();
		createUser = advertise.getCreateUser();
		modifyDate = advertise.getModifyDate();
		modifyUser = advertise.getModifyUser();
		mainCateId = position.getMainCateId().toString();
		showSeq = content.getShowSeq();
		readPropertise(content);
		return "editSpecialActAdv";
	}

	/**
	 * @return the advId
	 */
	public String getAdvId() {
		return advId;
	}

	/**
	 * @return the advName
	 */
	public String getAdvName() {
		return advName;
	}

	/**
	 * @return the advStatus
	 */
	public String getAdvStatus() {
		return advStatus;
	}

	public String getAdvTitle() {
		return advTitle;
	}

	/**
	 * @return the advType
	 */
	public String getAdvType() {
		return advType;
	}

	public File getBannerImg() {
		return bannerImg;
	}

	public String getBannerImgFolder() {
		return bannerImgFolder;
	}

	public String getBannerImgLink() {
		return bannerImgLink;
	}

	public String getBannerImgName() {
		return bannerImgName;
	}

	public List<Category> getCategoryRoot() {
		return categoryRoot;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	/**
	 * @return the content
	 */
	public GenerateContent getContent() {
		return content;
	}

	/**
	 * @return the endDate
	 */
	public String getEndDate() {
		return endDate;
	}

	public String getEndTime() {
		return endTime;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public String getCreateUser() {
		return createUser;
	}

	public Date getModifyDate() {
		return modifyDate;
	}

	public String getModifyUser() {
		return modifyUser;
	}

	public String getError() {
		return error;
	}

	public String getMainCateId() {
		return mainCateId;
	}

	public GeneratePosition getPosition() {
		return position;
	}

	public int getShowSeq() {
		return showSeq;
	}

	/**
	 * @return the startDate
	 */
	public String getStartDate() {
		return startDate;
	}

	public String getStartTime() {
		return startTime;
	}

	public String getTab1Brief() {
		return tab1Brief;
	}

	public String getTab1Name() {
		return tab1Name;
	}

	public String getTab1ProdId() {
		return tab1ProdId;
	}

	public String getTab2Brief() {
		return tab2Brief;
	}

	public String getTab2Name() {
		return tab2Name;
	}

	public String getTab2ProdId() {
		return tab2ProdId;
	}

	public String getTab3Brief() {
		return tab3Brief;
	}

	public String getTab3Name() {
		return tab3Name;
	}

	public String getTab3ProdId() {
		return tab3ProdId;
	}

	public String getTab4Brief() {
		return tab4Brief;
	}

	public String getTab4Name() {
		return tab4Name;
	}

	public String getTab4ProdId() {
		return tab4ProdId;
	}

	public String getTab5Brief() {
		return tab5Brief;
	}

	public String getTab5Name() {
		return tab5Name;
	}

	public String getTab5ProdId() {
		return tab5ProdId;
	}

	public String getTab6Brief() {
		return tab6Brief;
	}

	public String getTab6Name() {
		return tab6Name;
	}

	public String getTab6ProdId() {
		return tab6ProdId;
	}

	public String getTab7Brief() {
		return tab7Brief;
	}

	public String getTab7Name() {
		return tab7Name;
	}

	public String getTab7ProdId() {
		return tab7ProdId;
	}

	public String getTab8Brief() {
		return tab8Brief;
	}

	public String getTab8Name() {
		return tab8Name;
	}

	public String getTab8ProdId() {
		return tab8ProdId;
	}

	/**
	 * 初始化变数值
	 */
	void initVariables() {
		advertise = new GenerateAdvertise();
		position = new GeneratePosition();
		content = new GenerateContent();

		advTitle = "";
		bannerImg = null;
		bannerImgName = "";
		bannerImgFolder = "";
		bannerImgLink = "";
		tab1ProdId = "";
		tab1Brief = "";
		tab1Name = "";

		tab2ProdId = "";
		tab2Brief = "";
		tab2Name = "";

		tab3ProdId = "";
		tab3Brief = "";
		tab3Name = "";

		tab4ProdId = "";
		tab4Brief = "";
		tab4Name = "";

		tab5ProdId = "";
		tab5Brief = "";
		tab5Name = "";

		tab6ProdId = "";
		tab6Brief = "";
		tab6Name = "";

		tab7ProdId = "";
		tab7Brief = "";
		tab7Name = "";

		tab8ProdId = "";
		tab8Brief = "";
		tab8Name = "";

		// tab9ProdId = "";
		// tab9Brief = "";
		// tab9Name = "";
		//
		// tab10ProdId = "";
		// tab10Brief = "";
		// tab10Name = "";

		startDate = "";
		endDate = "";
		startTime = "00:00";
		endTime = "00:00";
		showSeq = 0;
		createDate = null;
		createUser = "";
		modifyDate = null;
		modifyUser = "";
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		clearErrorsAndMessages();

		if (null == categoryRoot)
			categoryRoot = categoryService.getChild(0l);
	}

	/**
	 * 從db讀取propertise資料
	 * 
	 * @param content
	 * @throws IOException
	 */
	void readPropertise(GenerateContent content) throws IOException {
		Properties prop = new Properties();
		Reader reader = new StringReader(content.getContents());
		prop.load(reader);
		reader.close();
		advTitle = StringUtils.defaultString((String) prop.get("advTitle"), "");
		if (prop.containsKey("bannerImgName")) {
			bannerImgName = StringUtils.defaultString(prop.get("bannerImgName").toString());
			bannerImgFolder = StringUtils.defaultString(prop.get("bannerImgFolder").toString(), "");
			bannerImgLink = StringUtils.defaultString(prop.get("bannerImgLink").toString(), "");
		}
		tab1ProdId = StringUtils.defaultString(prop.get("tab1ProdId").toString(), "");
		tab1Brief = StringUtils.defaultString(prop.get("tab1Brief").toString(), "");
		tab1Name = StringUtils.defaultString(prop.get("tab1Name").toString(), "");

		tab2ProdId = StringUtils.defaultString(prop.get("tab2ProdId").toString(), "");
		tab2Brief = StringUtils.defaultString(prop.get("tab2Brief").toString(), "");
		tab2Name = StringUtils.defaultString(prop.get("tab2Name").toString(), "");

		tab3ProdId = StringUtils.defaultString(prop.get("tab3ProdId").toString(), "");
		tab3Brief = StringUtils.defaultString(prop.get("tab3Brief").toString(), "");
		tab3Name = StringUtils.defaultString(prop.get("tab3Name").toString(), "");

		tab4ProdId = StringUtils.defaultString(prop.get("tab4ProdId").toString(), "");
		tab4Brief = StringUtils.defaultString(prop.get("tab4Brief").toString(), "");
		tab4Name = StringUtils.defaultString(prop.get("tab4Name").toString(), "");

		tab5ProdId = StringUtils.defaultString(prop.get("tab5ProdId").toString(), "");
		tab5Brief = StringUtils.defaultString(prop.get("tab5Brief").toString(), "");
		tab5Name = StringUtils.defaultString(prop.get("tab5Name").toString(), "");

		tab6ProdId = StringUtils.defaultString(prop.get("tab6ProdId").toString(), "");
		tab6Brief = StringUtils.defaultString(prop.get("tab6Brief").toString(), "");
		tab6Name = StringUtils.defaultString(prop.get("tab6Name").toString(), "");

		tab7ProdId = StringUtils.defaultString(prop.get("tab7ProdId").toString(), "");
		tab7Brief = StringUtils.defaultString(prop.get("tab7Brief").toString(), "");
		tab7Name = StringUtils.defaultString(prop.get("tab7Name").toString(), "");

		tab8ProdId = StringUtils.defaultString(prop.get("tab8ProdId").toString(), "");
		tab8Brief = StringUtils.defaultString(prop.get("tab8Brief").toString(), "");
		tab8Name = StringUtils.defaultString(prop.get("tab8Name").toString(), "");

		// tab9ProdId = StringUtils.defaultString(prop.get("tab9ProdId").toString(), "");
		// tab9Brief = StringUtils.defaultString(prop.get("tab9Brief").toString(), "");
		// tab9Name = StringUtils.defaultString(prop.get("tab9Name").toString(), "");
		//
		// tab10ProdId = StringUtils.defaultString(prop.get("tab10ProdId").toString(), "");
		// tab10Brief = StringUtils.defaultString(prop.get("tab10Brief").toString(), "");
		// tab10Name = StringUtils.defaultString(prop.get("tab10Name").toString(), "");
	}

	EventImage saveImgFile(File imgFile, String fileName, String advType) throws Exception {
		try {
			eventImage = new EventImage();
			eventImage.setImgType(fileName.substring(fileName.lastIndexOf("."), fileName.length()));
			eventImage = generateService.saveEventImage(eventImage);
			boolean result = GenerateHelper.uploadImage(eventImage, imgFile, advType);
			if (result)
				return eventImage;
			else
				return null;
		} catch (Exception e) {
			logger.info("exception :{}", e.getMessage());
		}
		return null;
	}

	String saveImgFile(File imgFile, String fileName, String advType, String imgName, String imgFolder) throws Exception {
		StringBuffer sb = new StringBuffer();
		try {
			String today = FastDateFormat.getInstance(EcConstant.DEFAULT_PATTERN_DATE).format(DateUtil.getToday().getTime());
			eventImage = new EventImage();
			eventImage.setImgType(fileName.substring(fileName.lastIndexOf("."), fileName.length()));
			eventImage = generateService.saveEventImage(eventImage);
			boolean result = GenerateHelper.uploadImage(eventImage, imgFile, advType);
			if (result) {
				sb.append(imgName + "=").append(eventImage.getUuid()).append(eventImage.getImgType()).append(lineBreak);
				sb.append(imgFolder + "=").append(today).append(lineBreak);
			} else {
				sb.append(imgName + "=").append(lineBreak);
				sb.append(imgFolder + "=").append(lineBreak);
			}
		} catch (Exception e) {
			logger.info("exception :{}", e.getMessage());
		}
		return sb.toString();
	}

	/**
	 * 储存广告
	 * 
	 * @return
	 * @throws Exception
	 * @throws IOException
	 * @throws ParseException
	 * @throws CoreException
	 * @throws NumberFormatException
	 */
	public String saveSpecialActAdv() throws NumberFormatException, CoreException, ParseException, IOException, Exception {
		double imgSize = 90;
		int imgWidth = 768;
		int imgHeight = 180;
		int error_image = 0;
		if (specialActAdvValidate()) {
			contents = new StringBuilder("");
			// upload file
			if (GenerateHelper.mkDir(advType)) {
				if (StringUtils.isBlank(advId)) {// add
					if (validateFile(bannerImg, "bannerImg", "大Banner图片", bannerImgName, imgSize, imgWidth, imgHeight)) {
						contents.append(saveImgFile(bannerImg, bannerImgName, advType, "bannerImgName", "bannerImgFolder"));
					} else {
						contents.append("bannerImgName=").append(lineBreak);
						contents.append("bannerImgFolder=").append(lineBreak);
						error_image++;
					}
				} else {// update
					if (null == bannerImg) {
						if (StringUtils.isNotBlank(bannerImgName))
							contents.append("bannerImgName=").append(bannerImgName).append(lineBreak);
						contents.append("bannerImgFolder=").append(bannerImgFolder).append(lineBreak);
					} else {
						if (validateFile(bannerImg, "bannerImg", "大Banner图片", bannerImgName, imgSize, imgWidth, imgHeight)) {
							contents.append(saveImgFile(bannerImg, bannerImgName, advType, "bannerImgName", "bannerImgFolder"));
						} else {
							contents.append("bannerImgName=").append(lineBreak);
							contents.append("bannerImgFolder=").append(lineBreak);
							error_image++;
						}
					}
				}
				contents.append("bannerImgLink=").append(bannerImgLink).append(lineBreak);
				contents.append("advTitle=").append(advTitle).append("\n");
				contents.append("tab1ProdId=").append(tab1ProdId).append("\n");
				contents.append("tab1Brief=").append(tab1Brief).append("\n");
				contents.append("tab1Name=").append(tab1Name).append("\n");

				contents.append("tab2ProdId=").append(tab2ProdId).append("\n");
				contents.append("tab2Brief=").append(tab2Brief).append("\n");
				contents.append("tab2Name=").append(tab2Name).append("\n");

				contents.append("tab3ProdId=").append(tab3ProdId).append("\n");
				contents.append("tab3Brief=").append(tab3Brief).append("\n");
				contents.append("tab3Name=").append(tab3Name).append("\n");

				contents.append("tab4ProdId=").append(tab4ProdId).append("\n");
				contents.append("tab4Brief=").append(tab4Brief).append("\n");
				contents.append("tab4Name=").append(tab4Name).append("\n");

				contents.append("tab5ProdId=").append(tab5ProdId).append("\n");
				contents.append("tab5Brief=").append(tab5Brief).append("\n");
				contents.append("tab5Name=").append(tab5Name).append("\n");

				contents.append("tab6ProdId=").append(tab6ProdId).append("\n");
				contents.append("tab6Brief=").append(tab6Brief).append("\n");
				contents.append("tab6Name=").append(tab6Name).append("\n");

				contents.append("tab7ProdId=").append(tab7ProdId).append("\n");
				contents.append("tab7Brief=").append(tab7Brief).append("\n");
				contents.append("tab7Name=").append(tab7Name).append("\n");

				contents.append("tab8ProdId=").append(tab8ProdId).append("\n");
				contents.append("tab8Brief=").append(tab8Brief).append("\n");
				contents.append("tab8Name=").append(tab8Name).append("\n");

				// contents.append("tab9ProdId=").append(tab9ProdId).append("\n");
				// contents.append("tab9Brief=").append(tab9Brief).append("\n");
				// contents.append("tab9Name=").append(tab9Name).append("\n");
				//
				// contents.append("tab10ProdId=").append(tab10ProdId).append("\n");
				// contents.append("tab10Brief=").append(tab10Brief).append("\n");
				// contents.append("tab10Name=").append(tab10Name).append("\n");

				if (StringUtils.isBlank(advId)) {
					advertise = new GenerateAdvertise();
					position = new GeneratePosition();
					content = new GenerateContent();
				} else {
					advertise = generateService.get(Long.parseLong(advId));
					position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
					content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
				}
				// set advertise
				advertise.setAdvType(advType);
				advertise.setAdvName(advName.trim());
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				advertise.setStartDate(dateFormat.parse(startDate + " " + startTime + ":00"));
				advertise.setEndDate(dateFormat.parse(endDate + " " + endTime + ":59"));
				if (error_image > 0)
					advertise.setAdvStatus("90");
				else
					advertise.setAdvStatus(advStatus);
				position.setMainCateId(Long.parseLong(mainCateId));
				content.setContents(contents.toString());
				content.setShowSeq(showSeq);

				if (StringUtils.countMatches(content.getContents(), "ProdId") == 8) {
					if (StringUtils.isBlank(advId)) {
						generateService.saveGenerateCombine(advertise, position, content);
					} else
						generateService.updateGenerateCombine(advertise, position, content);
				}
				contents.setLength(0);
				// 存檔後立刻執行產生檔案的邏輯
				generateService.generateSpecialAct(Long.valueOf(mainCateId));
			}
			advId = advertise.getId().toString();
			if (getFieldErrors().isEmpty())
				addActionMessage("储存成功");
			else
				addActionMessage("储存成功,部份栏位资料有误");
		}
		return editSpecialActAdv();
	}

	/**
	 * @param advId the advId to set
	 */
	public void setAdvId(String advId) {
		this.advId = advId;
	}

	/**
	 * @param advName the advName to set
	 */
	public void setAdvName(String advName) {
		this.advName = advName;
	}

	/**
	 * @param advStatus the advStatus to set
	 */
	public void setAdvStatus(String advStatus) {
		this.advStatus = advStatus;
	}

	public void setAdvTitle(String advTitle) {
		this.advTitle = advTitle;
	}

	/**
	 * @param advType the advType to set
	 */
	public void setAdvType(String advType) {
		this.advType = advType;
	}

	public void setBannerImg(File bannerImg) {
		this.bannerImg = bannerImg;
	}

	public void setBannerImgFolder(String bannerImgFolder) {
		this.bannerImgFolder = bannerImgFolder;
	}

	public void setBannerImgLink(String bannerImgLink) {
		this.bannerImgLink = bannerImgLink;
	}

	public void setBannerImgName(String bannerImgName) {
		this.bannerImgName = bannerImgName;
	}

	public void setCategoryRoot(List<Category> categoryRoot) {
		this.categoryRoot = categoryRoot;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(GenerateContent content) {
		this.content = content;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}

	public void setModifyDate(Date modifyDate) {
		this.modifyDate = modifyDate;
	}

	public void setModifyUser(String modifyUser) {
		this.modifyUser = modifyUser;
	}

	public void setError(String error) {
		this.error = error;
	}

	/**
	 * @param generateService the generateService to set
	 */
	public void setGenerateService(GenerateService generateService) {
		this.generateService = generateService;
	}

	public void setMainCateId(String mainCateId) {
		this.mainCateId = mainCateId;
	}

	public void setPosition(GeneratePosition position) {
		this.position = position;
	}

	public void setShowSeq(int showSeq) {
		this.showSeq = showSeq;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public void setTab1Brief(String tab1Brief) {
		this.tab1Brief = tab1Brief;
	}

	public void setTab1Name(String tab1Name) {
		this.tab1Name = tab1Name;
	}

	public void setTab1ProdId(String tab1ProdId) {
		this.tab1ProdId = tab1ProdId;
	}

	public void setTab2Brief(String tab2Brief) {
		this.tab2Brief = tab2Brief;
	}

	public void setTab2Name(String tab2Name) {
		this.tab2Name = tab2Name;
	}

	public void setTab2ProdId(String tab2ProdId) {
		this.tab2ProdId = tab2ProdId;
	}

	public void setTab3Brief(String tab3Brief) {
		this.tab3Brief = tab3Brief;
	}

	public void setTab3Name(String tab3Name) {
		this.tab3Name = tab3Name;
	}

	public void setTab3ProdId(String tab3ProdId) {
		this.tab3ProdId = tab3ProdId;
	}

	public void setTab4Brief(String tab4Brief) {
		this.tab4Brief = tab4Brief;
	}

	public void setTab4Name(String tab4Name) {
		this.tab4Name = tab4Name;
	}

	public void setTab4ProdId(String tab4ProdId) {
		this.tab4ProdId = tab4ProdId;
	}

	public void setTab5Brief(String tab5Brief) {
		this.tab5Brief = tab5Brief;
	}

	public void setTab5Name(String tab5Name) {
		this.tab5Name = tab5Name;
	}

	public void setTab5ProdId(String tab5ProdId) {
		this.tab5ProdId = tab5ProdId;
	}

	public void setTab6Brief(String tab6Brief) {
		this.tab6Brief = tab6Brief;
	}

	public void setTab6Name(String tab6Name) {
		this.tab6Name = tab6Name;
	}

	public void setTab6ProdId(String tab6ProdId) {
		this.tab6ProdId = tab6ProdId;
	}

	public void setTab7Brief(String tab7Brief) {
		this.tab7Brief = tab7Brief;
	}

	public void setTab7Name(String tab7Name) {
		this.tab7Name = tab7Name;
	}

	public void setTab7ProdId(String tab7ProdId) {
		this.tab7ProdId = tab7ProdId;
	}

	public void setTab8Brief(String tab8Brief) {
		this.tab8Brief = tab8Brief;
	}

	public void setTab8Name(String tab8Name) {
		this.tab8Name = tab8Name;
	}

	public void setTab8ProdId(String tab8ProdId) {
		this.tab8ProdId = tab8ProdId;
	}

	private boolean specialActAdvValidate() throws CoreException, ParseException, IOException {
		/**************************** 必填栏位 ****************************/
		logger.info("advName = " + advName);

		if (StringUtils.isBlank(advName))
			addFieldError("advName", "请输入广告名称");

		if (StringUtils.isBlank(advTitle))
			addFieldError("advTitle", "请输入广告标题");

		if (StringUtils.isBlank(startDate))
			addFieldError("startDate", "请输入起始日期");

		if (StringUtils.isBlank(endDate))
			addFieldError("endDate", "请输入结束日期");

		if (StringUtils.isBlank(mainCateId))
			addFieldError("mainCateId", "请选择馆别");

		// 1
		if (StringUtils.isBlank(tab1ProdId))
			addFieldError("tab1ProdId", "请输入商品1编号");
		else if (!generateService.isProductExist(tab1ProdId))
			error = "1";

		// 2
		if (StringUtils.isBlank(tab2ProdId))
			addFieldError("tab2ProdId", "请输入商品2编号");
		else if (!generateService.isProductExist(tab2ProdId))
			error = (StringUtils.isNotBlank(error) ? error + ",2" : "2");

		// 3
		if (StringUtils.isBlank(tab3ProdId))
			addFieldError("tab3ProdId", "请输入商品3编号");
		else if (!generateService.isProductExist(tab3ProdId))
			error = (StringUtils.isNotBlank(error) ? error + ",3" : "3");

		// 4
		if (StringUtils.isBlank(tab4ProdId))
			addFieldError("tab4ProdId", "请输入商品4编号");
		else if (!generateService.isProductExist(tab4ProdId))
			error = (StringUtils.isNotBlank(error) ? error + ",4" : "4");

		// 5
		if (StringUtils.isBlank(tab5ProdId))
			addFieldError("tab5ProdId", "请输入商品5编号");
		else if (!generateService.isProductExist(tab5ProdId))
			error = (StringUtils.isNotBlank(error) ? error + ",5" : "5");

		// 6
		if (StringUtils.isBlank(tab6ProdId))
			addFieldError("tab6ProdId", "请输入商品6编号");
		else if (!generateService.isProductExist(tab6ProdId))
			error = (StringUtils.isNotBlank(error) ? error + ",6" : "6");

		// 7
		if (StringUtils.isBlank(tab7ProdId))
			addFieldError("tab7ProdId", "请输入商品7编号");
		else if (!generateService.isProductExist(tab7ProdId))
			error = (StringUtils.isNotBlank(error) ? error + ",7" : "7");

		// 8
		if (StringUtils.isBlank(tab8ProdId))
			addFieldError("tab8ProdId", "请输入商品8编号");
		else if (!generateService.isProductExist(tab8ProdId))
			error = (StringUtils.isNotBlank(error) ? error + ",8" : "8");

		// 9
		// if (StringUtils.isBlank(tab9ProdId))
		// addFieldError("tab9ProdId", "请输入商品9编号");
		// else if (!generateService.isProductExist(tab9ProdId))
		// error = (StringUtils.isNotBlank(error) ? error + ",9" : "9");
		//
		// // 10
		// if (StringUtils.isBlank(tab10ProdId))
		// addFieldError("tab10ProdId", "请输入商品10编号");
		// else if (!generateService.isProductExist(tab10ProdId))
		// error = (StringUtils.isNotBlank(error) ? error + ",10" : "10");

		if (getFieldErrors().isEmpty() && StringUtils.isBlank(error))
			return true;
		else
			return false;
	}

	boolean validateFile(File imgFile, String fieldName, String fieldNameMsg, String fileName, double fileSize,
			int fileWidth, int fileHeight) throws IOException {
		int error_size = 0;
		if (null != imgFile) {
			if ((imgFile.length() / 1024) > fileSize) {
				addFieldError(fieldName, fieldNameMsg + "檔案尺寸不符");
				error_size++;
			}
			// flash檔不檢查長寬
			if (!StringUtils.endsWithIgnoreCase(fileName, ".swf")) {
				Image image = ImageIO.read(imgFile);
				if (image.getHeight(null) > fileHeight || image.getWidth(null) > fileWidth) {
					addFieldError(fieldName, fieldNameMsg + "檔案长度/宽度不符");
					error_size++;
				}
			}
			if (error_size > 0)
				return false;
			else
				return true;
		} else
			return false;
	}
}
